package kr.pe.smallhope.mobismap;

import kr.pe.smallhope.mobismap.utils.HttpUtil;

import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
    public static final String TITLE = "Map Utility for Mobis";
    
    public ApplicationWorkbenchWindowAdvisor(
            IWorkbenchWindowConfigurer configurer) {
        super(configurer);
    }
    
    public ActionBarAdvisor createActionBarAdvisor(
            IActionBarConfigurer configurer) {
        return new ApplicationActionBarAdvisor(configurer);
    }
    
    public void preWindowOpen() {
        IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
        configurer.setInitialSize(new Point(1024, 768));
        configurer.setShowCoolBar(false);
        configurer.setShowStatusLine(true);
        configurer.setShowProgressIndicator(true);
        configurer.setTitle(TITLE);
        
        initMobisMapConstants();
    }

    /**
     * 
     */
    private void initMobisMapConstants() {
        MobisMapConstants.setProperty(MobisMapConstants.KEY_NAVER_MAP_USER_KEY, "3b77fb66a0579d6a41ee5435f9b5069f");
        MobisMapConstants.setProperty(MobisMapConstants.KEY_GOOGLE_MAP_USER_KEY, "ABQIAAAAUCjr6FPQiYbyTzQ5Tr2HmhTp86QSx33iE4CpBJdDH5t4gXyx0RTDsXBZR9F_-TymhIvA5I69L-JEAg");

        String response = HttpUtil.request(MobisMapConstants.URL + "keyinfo.html");
        if (response != null) {
            String[] strings = response.split(" ");
            if ("KeYsTaRt".equals(strings[0])) {
                MobisMapConstants.setProperty(strings[1], strings[2]);
                MobisMapConstants.setProperty(strings[3], strings[4]);
            }            
        }
        
//        String ret = HttpUtil.request("http://map.naver.com/findRoute2/");
//        //var routeKey = "tLbLLxj2bPNAG7Mg00byetjVTkuLbzfZRNxt4xOs1Rs%3D";
//        String fromText = "var routeKey = \"";
//        String toText = "\";";
//        int from = ret.indexOf(fromText) + fromText.length();
//        int to = ret.indexOf(toText, from);
//        String routeKey = ret.substring(from, to);
////        System.err.println("Naver Route Key [" + routeKey + "]");
//        MobisMapConstants.setProperty(MobisMapConstants.KEY_NAVER_MAP_ROUTE_KEY, routeKey);
    }
}
